/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》  http://lblbc.cn/note
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 */
const { sequelize } = require('@core/db')
const { QueryTypes } = require('sequelize');
const { Order } = require('@models/order')
const { UserOrder } = require('@models/user_order')
const { v4: uuidv4 } = require("uuid");

class OrderDao {

  static async createOrder(userId, simpleCartItemList) {
    const userOrder = new UserOrder();
    userOrder.user_id = userId;
    userOrder.id = uuidv4();
    userOrder.create_time = Date.now();
    userOrder.status = 1;
    const res = await userOrder.save();
    simpleCartItemList.every((simpleCartItem) => {
      var i = simpleCartItem["id"];
      const order = new Order();
      order.id = uuidv4();
      order.goods_id = simpleCartItem["goodsId"];
      order.order_id = userOrder.id;
      order.quantity = simpleCartItem["quantity"]
      order.save();
    })
    return [null, userOrder.id];
  }

  static async queryOrder(orderId) {
    try {
      const result = await sequelize.query("select * from m_user_order where id = '" + orderId + "'", { type: QueryTypes.SELECT });
      const firstRow = result[0]
      const queryOrderResp = new Object();
      queryOrderResp['orderId'] = firstRow.id;
      queryOrderResp['createTime'] = firstRow.create_time;
      queryOrderResp['status'] = firstRow.status;

      const orderDetailResult = await sequelize.query("SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id = '" + orderId + "'", { type: QueryTypes.SELECT });
      queryOrderResp['list'] = orderDetailResult

      return [null, queryOrderResp]
    } catch (err) {
      return [err, null]
    }
  }

  static async queryByStatus(userId, orderStatus) {
    try {
      const result = await sequelize.query("select * from m_user_order where user_id = " + userId, { type: QueryTypes.SELECT });
      const resp = new Array();

      for (let i = 0; i < result.length; i++) {
        var userOrder = result[i];
        const queryOrderResp = new Object();
        queryOrderResp['orderId'] = userOrder.id;
        queryOrderResp['createTime'] = userOrder.create_time;
        queryOrderResp['status'] = userOrder.status;

        const orderDetailResult = await sequelize.query("SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id = '" + userOrder.id + "'", { type: QueryTypes.SELECT });
        queryOrderResp['list'] = orderDetailResult
        resp.push(queryOrderResp);
      }
      return [null, resp]
    } catch (err) {
      return [err, null]
    }
  }

  static async deleteOrder(orderId) {
    try {
      await sequelize.query("delete from m_orders where order_id = '" + orderId + "'", { type: QueryTypes.DELETE });
      await sequelize.query("delete from m_user_order where id = '" + orderId + "'", { type: QueryTypes.DELETE });
      return [null, '']
    } catch (err) {
      return [err, null]
    }
  }

}

module.exports = {
  OrderDao
}
